﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataService;
using System.Data;
using System.Data.SqlClient;
using RM2012Service.Business;

namespace RM2012Service.DataAccess
{
    public class InvoiceDetailDAO
    {
        DBConnection db;

        public InvoiceDetailDAO()
        {
            db = new DBConnection();
        }

        public DataSet GetInvoiceDetailByInvoiceId(string invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            return db.ExecuteDataSet("GetDetailByInvoiceIdFull", "InvoiceDetail", param);
        }

        public DataSet GetByMenuAndInvoice(String invoiceId, String menuId)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = invoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = menuId;

            return db.ExecuteDataSet("GetDetailByMenuAndInvoice", param);
        }

        public int Add(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            param[2] = new SqlParameter("@State", SqlDbType.Bit);
            param[2].Value = info.State;

            param[3] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[3].Value = info.Quantity;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[5].Value = info.IsDeleted;

            return db.ExecuteNoneQuery("AddDetail", param);
        }

        public int Update(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            param[2] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[2].Value = info.Quantity;

            param[3] = new SqlParameter("@State", SqlDbType.Bit);
            param[3].Value = info.State;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            return db.ExecuteNoneQuery("UpdateDetail", param);
        }

        public int Delete(InvoiceDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@MenuId", SqlDbType.VarChar, 6);
            param[1].Value = info.MenuId;

            return db.ExecuteNoneQuery("DeleteDetail", param);
        }
    }
}
